import { message } from "@/utils/message";
import { computed } from "vue";
import { updateVideoStatus } from "@/api/videoInfo";

// 需是hooks写法（函数中有return），避免失去响应性
export function useColumns(
  videoList: any,
  handleViewCallback?: (row: any) => void,
  handleEditCallback?: (row: any) => void,
  handleRefreshCallback?: () => void
) {
  const tableData = computed(() => videoList.value || []);
  const columns: TableColumnList = [
    {
      label: "视频名称",
      prop: "video_name"
    },
    {
      label: "审批状态",
      prop: "status",
      width: 120,
      align: "center",
      cellRenderer: ({ row }) => {
        let statusText = "";
        let statusType = "";

        switch (row.status) {
          case 0:
            statusText = "审核中";
            statusType = "warning";
            break;
          case 1:
            statusText = "已通过";
            statusType = "success";
            break;
          case 2:
            statusText = "已退回";
            statusType = "danger";
            break;
          case 3:
            statusText = "待提交";
            statusType = "warning";
            break;
          default:
            statusText = "未知";
            statusType = "info";
        }

        return <el-tag type={statusType}>{statusText}</el-tag>;
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 220,
      cellRenderer: ({ row }) => (
        <>
          <el-button size="small" onClick={() => handleView(row)}>
            查看
          </el-button>
          <el-button size="small" onClick={() => handleEdit(row)}>
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            onClick={() => handleSubmitAudit(row)}>
            提交审批
          </el-button>
        </>
      )
    }
  ];

  const handleView = (row: any) => {
    if (handleViewCallback) {
      handleViewCallback(row);
    } else {
      message(`您查看了数据：${JSON.stringify(row)}`, {
        type: "success"
      });
    }
  };

  const handleEdit = (row: any) => {
    // 统一拦截提示：0 审核中、1 已通过
    if (row.status === 0) {
      message("视频正在审核中，不能修改", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("视频已通过，不能修改", { type: "warning" });
      return;
    }
    if (handleEditCallback) {
      handleEditCallback(row);
    } else {
      message(`您修改了数据：${JSON.stringify(row)}`, {
        type: "success"
      });
    }
  };

  const handleSubmitAudit = async (row: any) => {
    // 状态：0=审核中，1=已通过，2=未通过，3=待提交
    if (row.status === 0) {
      message("已提交，不可重复审批", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("已通过，不可重复审批", { type: "warning" });
      return;
    }
    if (row.status !== 2 && row.status !== 3) {
      message("只有未通过或待提交状态才能提交审批", { type: "warning" });
      return;
    }
    try {
      const payload = { id: row.id, exhibitor_id: row.exhibitor_id, status: 0 };
      const res: any = await updateVideoStatus(payload);
      if (res.code === 1) {
        message("提交审批成功！", { type: "success" });
        // 提交审批成功后刷新列表
        if (handleRefreshCallback) {
          handleRefreshCallback();
        }
      } else {
        message(res.msg || "提交审批失败", { type: "error" });
      }
    } catch (e) {
      console.error(e);
      message("提交审批失败", { type: "error" });
    }
  };

  return {
    columns,
    tableData
  };
}
